pub fn do_work() {
    fn plus_one(x: Option<i32>) -> Option<i32> {
        match x {
            None => None,
            Some(i) => Some(i + 1),
        }
    }

    let five = Some(5);
    let six = plus_one(five);
    let none = plus_one(None);
}

pub mod on_err {
    use std::fs::File;
    use std::io::{self, ErrorKind, Read};

    pub fn crash() {
        panic!("crash")
    }

    pub fn slice_out_range() {
        let v = vec![1, 2, 3];
        v[99];
    }

    pub fn base_file() {
        let greeting_file_result = File::open("hello.txt");

        let greeting_file = match greeting_file_result {
            Ok(file) => file,
            Err(error) => match error.kind() {
                ErrorKind::NotFound => match File::create("hello.txt") {
                    Ok(fc) => fc,
                    Err(e) => panic!("Problem creating the file: {:?}", e),
                },
                other_error => {
                    panic!("Problem opening the file: {:?}", other_error);
                }
            },
        };
    }

    pub fn op_file() {
        let file = File::open("hello.txt").unwrap();
        let file = File::open("hello.txt").expect("hello.txt should be included in this project");
        let file = File::open("hello.txt")
            .unwrap_or_else(|error| panic!("Problem opening the file: {:?}", error));
    }

    fn read_username_long() -> Result<String, io::Error> {
        let username_file_result = File::open("hello.txt");

        let mut username_file = match username_file_result {
            Ok(file) => file,
            Err(e) => return Err(e),
        };

        let mut username = String::new();

        match username_file.read_to_string(&mut username) {
            Ok(_) => Ok(username),
            Err(e) => Err(e),
        }
    }

    fn read_username_short() -> Result<String, io::Error> {
        // 错误冒泡, 只处理期望的正常情况
        let mut username_file = File::open("hello.txt")?;
        let mut username = String::new();
        username_file.read_to_string(&mut username)?;
        Ok(username)
    }
}

pub fn first_word(s: &str) -> &str {
    for (index, c) in s.char_indices() {
        if c == ' ' {
            print!("first_word.char_indices");
            return &s[0..index];
        }
    }

    for (i, &item) in s.as_bytes().iter().enumerate() {
        if item == b' ' {
            print!("first_word.iter.enumerate");
            return &s[0..i];
        }
    }

    &s[..]
}

pub mod ls16 {
    use std::thread;
    use std::time::Duration;
    pub fn run() {
        let v = vec![1, 2, 3];

        let handle = thread::spawn(move || {
            eprintln!("Here's a vector: {:?}", v);
            thread::sleep(Duration::from_millis(1));
        });

        handle.join().unwrap();
        eprintln!("Done")
    }

    #[test]
    fn run_test() {
        run();
        assert_eq!(1, 1);
    }
}
